package com.wql.img;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImgUtils_ {
	private File _saveFile;
	private BufferedImage _sourceBufferedImage;

	public static final int LETF = 1;
	public static final int RIGHT = 2;
	public static final int CENTER = 3;
	public static final int ABOVE = 4;
	public static final int BELOW = 5;

	private BufferedImage _waterMarker;

	private int markerX;
	private int markerY;

	private int markerWidth;
	private int markerHeight;

	public ImgUtils_(String soursePath, String savePath) throws IOException {
		this(new File(soursePath), new File(savePath));
	}

	public ImgUtils_(File _sourseFile, File _saveFile) throws IOException {
		if (_sourseFile == null || _saveFile == null) {
			throw new NullPointerException("图片文件不能为null");
		}
		if (!_sourseFile.exists()) {
			throw new FileNotFoundException(_sourseFile.getAbsolutePath() + "not found!");
		}
		this._sourceBufferedImage = ImageIO.read(_sourseFile);
		this._saveFile = _saveFile;
	}

	public void compress(int width, int height) throws IOException {
		// ---缩放图片
		Image _compressImage = _sourceBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		// ----图像透明,设置
		BufferedImage _bufferedImage = new BufferedImage(width, height, this._sourceBufferedImage.getType());
		Graphics graphics = _bufferedImage.getGraphics();
		Graphics2D graphics2d = (Graphics2D) graphics;
		// ---绘制图片
		graphics2d.drawImage(_compressImage, 0, 0, null);
		saveImg(_bufferedImage);

	}

	public void compress(float radio) throws IOException {
		int _width = _sourceBufferedImage.getWidth();
		int _height = _sourceBufferedImage.getHeight();
		float _toWidth = _width;
		float _toHeight = _height;
		if (radio != 0) {
			_toWidth *= radio;
			_toHeight *= radio;
		}
		compress(Math.round(_toWidth), Math.round(_toHeight));
	}

	public void setWaterMarker(String imgPath, int x, int y, int width, int height) throws IOException {
		this._waterMarker = ImageIO.read(new File(imgPath));
		this.markerX = x;
		this.markerY = y;
		this.markerWidth = width;
		this.markerHeight = height;
	}

	public void zoom(int width, int height, boolean isCompress) throws IOException {
		if (isCompress) {
			compress(width, height);
		} else {
			BufferedImage _bufferedImage = new BufferedImage(width, height, this._sourceBufferedImage.getType());
			Graphics graphics = _bufferedImage.getGraphics();
			Graphics2D graphics2d = (Graphics2D) graphics;
			// ---绘制图片
			graphics2d.drawImage(this._sourceBufferedImage, 0, 0, width, height, null);
			// --
			saveImg(_bufferedImage);
		}
	}

	public void zoom(float radio, boolean isCompress) throws IOException {
		int _width = _sourceBufferedImage.getWidth();
		int _height = _sourceBufferedImage.getHeight();
		float _toWidth = _width;
		float _toHeight = _height;
		if (radio != 0) {
			_toWidth *= radio;
			_toHeight *= radio;
		}
		zoom(Math.round(_toWidth), Math.round(_toHeight), isCompress);
	}

	public String getFileSuffix(File file) {
		if (file == null) {
			return null;
		}

		return getFileSuffix(file.getName());
	}

	public String getFileSuffix(String fileName) {
		if (fileName == null) {
			return null;
		}
		String suffix = "";
		int i = fileName.lastIndexOf('.');
		if (i > 0 && i < fileName.length() - 1) {
			suffix = fileName.substring(i + 1);
		}
		return suffix;
	}

	public void setSourceFile(File sourseFile) throws IOException {
		this._sourceBufferedImage = ImageIO.read(sourseFile);
	}

	public void setSourceFile(String path) throws IOException {
		setSaveFile(new File(path));
	}

	public void setSaveFile(File _saveFile) {
		this._saveFile = _saveFile;
	}

	public void setSaveFile(String path) {
		setSaveFile(new File(path));
	}

	public void saveImg(BufferedImage bufferedImage) throws IOException {
		Graphics2D graphics2d = (Graphics2D) bufferedImage.getGraphics();
		// ----水印图片不等于NULL，给图片添加水印
		if (this._waterMarker != null) {
			if (bufferedImage.getWidth() < this.markerWidth || bufferedImage.getHeight() < this.markerHeight) {
			} else {
				// ---绘制水印
				graphics2d.drawImage(this._waterMarker, markerX, markerY, markerWidth, markerHeight, null);
			}
		}
		graphics2d.dispose();
		ImageIO.write(bufferedImage, getFileSuffix(_saveFile), _saveFile);
	}

	// ---测试
	public static void main(String[] args) {
		try {
			ImgUtils_ ifs = new ImgUtils_("D:/1.jpg", "D:/2.jpg");
			ifs.zoom(100, 800, false);
			// ifs.compress(200, 200);
			System.out.println("成功！！！");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
